package com.atguigu.es;

import com.atguigu.beans.Employee;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.MetricAggregation;
import io.searchbox.core.search.aggregation.TermsAggregation;

import java.io.IOException;
import java.util.List;

public class Select {
    public static void main(String[] args) throws IOException {
        //一.创建和es数据库的连接,获取一个客户端

        //1. new出这个工厂
        JestClientFactory factory = new JestClientFactory();
        //2. 看源码这个工厂有连接es地址的默认属性,得主动为属性赋值,否则连不上
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();
        factory.setHttpClientConfig(httpClientConfig);
        //3.通过工厂得到jestclient
        JestClient client = factory.getObject();

        String request = "{\n" +
                "  \"query\": {\n" +
                "    \"match\": {\n" +
                "      \"hobby\": \"购物\"\n" +
                "    }\n" +
                "  }, \n" +
                "   \"aggs\": {\n" +
                "     \"emps_count\": {\n" +
                "       \"terms\": {\n" +
                "         \"field\": \"gender.keyword\",\n" +
                "         \"size\": 10\n" +
                "       },\n" +
                "       \"aggs\": {\n" +
                "         \"gender_avg_age\": {\n" +
                "           \"avg\": {\n" +
                "             \"field\": \"age\"\n" +
                "           }\n" +
                "         }\n" +
                "       }\n" +
                "     }\n" +
                "   }\n" +
                "}";
        //二.中间操作
        //  1.传入请求体给建造的查询对象
        Search search = new Search.Builder(request).addIndex("test").addType("emps").build();
        //  2.执行查询结果
        SearchResult searchResult = client.execute(search);
        System.out.println("total:"+searchResult.getTotal());
        System.out.println("max_score:"+searchResult.getMaxScore());
        //  3.根据响应体获取相应的查询结果
            //3.1 拿取每个hit目标
        List<SearchResult.Hit<Employee, Void>> hits = searchResult.getHits(Employee.class);

        for (SearchResult.Hit<Employee, Void> hit : hits) {
            System.out.println(hit.index + "," + hit.type + hit.id + "," + hit.score);
            System.out.println(hit.source);
        }
            //3.2 拿取agg
        MetricAggregation aggregations = searchResult.getAggregations();
        TermsAggregation emps_count = aggregations.getTermsAggregation("emps_count");
        List<TermsAggregation.Entry> buckets = emps_count.getBuckets();
        for (TermsAggregation.Entry bucket : buckets) {
            System.out.println(bucket.getKey() + "," + bucket.getCount() + "," + bucket.getAvgAggregation("gender_avg_age").getAvg());
        }

        //关闭客户端
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
